package com.dehui.traders.ui.stockdetail.chart;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.kchart.DataParse;
import com.dehui.traders.kchart.KLineManager;
import com.dehui.traders.kchart.MyCombinedChart;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.ui.OptionHelper;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class ZhouKDetailFragment extends Fragment {

    private View view;
    private String code;
    private String market;
    private String close;
    private MyCombinedChart mChartKline;
    private MyCombinedChart mChartKVolume;
    private View llK;
    private View rlLoading;
    private int zhou;
    public List<String[]> mZhouKList = new ArrayList<>();
    private boolean mBsSelect = true;
    private DataParse mData;
    private StocksDetailActivity mActivity;
    private boolean isComplete;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null)
            view = inflater.inflate(R.layout.fragment_detail_k, container, false);

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        code = bundle.getString("code");
        market = bundle.getString("market");
        close = bundle.getString("close");
        zhou = bundle.getInt("zhou");
        mActivity = (StocksDetailActivity) getActivity();
        initViews();
        initLesionr();

        if (zhou == 0){
            if (mZhouKList != null && mZhouKList.size() > 0){
                setZhouK();
            }else{
                getZhouk();
            }
        }
    }

    private void getZhouk() {
        if (mZhouKList == null || mZhouKList.size() <= 0){
            List<String[]> list = OptionHelper.getZhouKLint(getActivity(), code, 0);
            if (list != null){
                mZhouKList.clear();
                mZhouKList.addAll(list);
                setZhouK();
            }
            getZhoukNet();
        }else{
           setZhouK();
        }
    }

    private void getZhoukNet() {
        Call<BaseListResponse<String[]>> call = NetImpl.getBussinessLabellwPoint(market + code);
        call.enqueue(new CallListener<BaseListResponse<String[]>>() {
            @Override
            public void _onResponse(Response<BaseListResponse<String[]>> response) {
                BaseListResponse<String[]> baseListResponse = response.body();
                if (baseListResponse.status == 200){
                    List<String[]> list = baseListResponse.result;
                    Collections.reverse(list);
                    mZhouKList.clear();
                    mZhouKList.addAll(list);
                    setZhouK();
                    OptionHelper.saveZhouKLine(getActivity(),code,0,new Gson().toJson(mZhouKList));
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }



    private void setZhouK() {
        llK.setVisibility(View.VISIBLE);
        rlLoading.setVisibility(View.GONE);
        mData = new DataParse();
        mData.parseKLineBS(mZhouKList,mBsSelect);
        KLineManager.getInstange().initKLine(getActivity(), mChartKline, mChartKVolume, mData);
        isComplete = true;
    }

    private void initViews() {
        mChartKline = view.findViewById(R.id.mycombinedchart_k_line);
        mChartKVolume = view.findViewById(R.id.mycombinedchart_k_volume);
        llK = view.findViewById(R.id.ll_k);
        rlLoading = view.findViewById(R.id.rl_loading);
    }

    public void setBs(boolean selected) {
        mBsSelect = selected;
        if (isComplete)
            setZhouK();
    }

    private void initLesionr() {
        mChartKline.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() - mChartKline.getHeight();
                Highlight h1 = mChartKVolume.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartKVolume.highlightValues(new Highlight[]{highlight});
                mActivity.showKView(h.getXIndex(),mData);
            }

            @Override
            public void onNothingSelected() {
                mActivity.dimissKView();
                mChartKVolume.highlightValue(null);
            }
        });

        mChartKVolume.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() + mChartKline.getHeight();
                Highlight h1 = mChartKline.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartKline.highlightValues(new Highlight[]{highlight});
                mActivity.showKView(h.getXIndex(),mData);
            }

            @Override
            public void onNothingSelected() {
                mActivity.dimissKView();
                mChartKline.highlightValue(null);
            }
        });
    }

}
